from eventServer import eventServer
from cCube import CCube
from vec2 import Vec2

class RCube(CCube):
    
    def __init__(self):
        CCube.__init__(self)
        
        eventServer.registerToFront("update", self.update)
        self.setVar("base", "rCube")
        self.setVar("cActive", True)
        self.setVar("ignoreSurface", None)
        self.aboveThresh = 0.25
        self.blink = False
        self.blinkInterval = 0.05
        self.blinkCount = 0
        self.blinkOn = True
        
        self.setVar("vel", Vec2(0,0))
        
    def setIgnoreSurface(self, object, side):
        self.setVar("ignoreSurface", [object, side])
   
    def clearIgnoreSurface(self):
        self.setVar("ignoreSurface", None)
        
    def getBase(self, obj):
        if(obj.hasVar('base')):
            b = obj.getVar('base')
        else:
            b = "None"
        return b
        
    def isAbove(self, obj, side, pos):
        if(side == 0):
            norm = obj.getVar("xNorm")
            width1 = (obj.getVar("scale").x/2.0) - self.aboveThresh
            width2 = (self.getVar("scale").x/2.0) - self.aboveThresh
        elif(side == 1):
            norm = obj.getVar("yNorm")
            width1 = (obj.getVar("scale").y/2.0) - self.aboveThresh
            width2 = (self.getVar("scale").y/2.0) - self.aboveThresh
        elif(side == 2):
            norm = -1 * obj.getVar("xNorm")
            width1 = (obj.getVar("scale").x/2.0) - self.aboveThresh
            width2 = (self.getVar("scale").x/2.0) - self.aboveThresh
        elif(side == 3):
            norm = -1 * obj.getVar("yNorm")
            width1 = (obj.getVar("scale").y/2.0) - self.aboveThresh
            width2 = (self.getVar("scale").y/2.0) - self.aboveThresh

        op = obj.getVar("pos")
        vec = Vec2( (pos.x - (norm.x * width2)) - (op.x + (norm.x * width1)), (pos.y - (norm.y * width2)) - (op.y + (norm.y * width1)) ).norm()
        d = norm.dot(vec)
        if(d > -0.001):
            a = True
        else:
            a = False
            
        return a
        
    def isToward(self, v, obj, angle):
        if(obj.hasVar("vel")):
            ov = obj.getVar("vel")
            tv = v - ov
        else:
            tv = v
        d = angle.dot(tv.norm())
        if(d >= -0.10):
            t = True
        else:
            t = False
            
        return t
        
    def isIgnoreSurface(self, ignore, obj, side):
        iSurf = False
        if(not(ignore == None)):
            if(ignore[0] == obj and ignore[1] == side):
                iSurf = True
        return iSurf
        
    def update(self, cmd, args):
        v  = self.getVar("vel")
        p  = self.getVar("pos")
        ig = self.getVar("ignoreSurface")
        dt = args["dtime"]

        self.blinkCube(dt)

        if(self.hasVar("collisions")):
            for dist, vec, side, obj in self.getVar("collisions"):
                
                sideType = obj.getVar("sides")[side]
                above = self.isAbove(obj, side, p)
                toward = self.isToward(v, obj, vec)
                ignoreSurface = self.isIgnoreSurface(ig, obj, side)
                            
                self.onCollision(dist, vec, side, sideType, above, toward, ignoreSurface, obj)
                
    def onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object):
        pass
    
    def blinkCube(self, dt):
        if(self.blink):
            self.blinkCount -= dt
            
            if(self.blinkCount < 0):
                self.blinkCount = self.blinkInterval
                if(self.blinkOn):
                    self.blinkOn = False
                    self.setVar("shown", True)
                else:
                    self.blinkOn = True
                    self.setVar("shown", False)